<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>实时日志查看器</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        #logContainer {
            height: 800px;
            overflow-y: auto;
            border: 1px solid #ccc;
            padding: 10px;
            margin-top: 20px;
            font-family: Consolas, monospace;
            white-space: pre-wrap;
        }
        pre {
            margin: 0;
            line-height: 1.5;
        }
        .error {
            color: red;
        }
        button {
            margin-right: 10px;
            padding: 5px 10px;
            font-size: 16px;
        }
    </style>
    <!-- 引入 SockJS 和 STOMP 客户端 -->
    <script src="https://cdn.jsdelivr.net/npm/sockjs-client@1/dist/sockjs.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/stompjs@2.3.3/lib/stomp.min.js"></script>
</head>
<body>
<h1>实时日志查看器</h1>
<button id="toggleConnectionBtn">停止接收日志</button>
<button id="clearBtn">清空日志</button>
<button id="downloadBtn">下载日志</button>
<div id="logContainer"></div>

<script>
    var stompClient = null;
    var logContainer = document.getElementById('logContainer');
    var isConnected = false;
    var lineNumber = 1; // 初始化行号

    function connect() {
        var socket = new SockJS('/ws');
        stompClient = Stomp.over(socket);
        stompClient.connect({}, function(frame) {
            isConnected = true;
            console.log("Connected: " + frame);
            stompClient.subscribe('/topic/logs', function(message) {
                var log = JSON.parse(message.body);
                displayLog(log);
            });
        });
    }

    function disconnect() {
        if (stompClient !== null) {
            stompClient.disconnect(function() {
                isConnected = false;
                console.log("Disconnected");
            });
        }
    }

    document.getElementById('toggleConnectionBtn').addEventListener('click', function () {
        if (isConnected) {
            disconnect();
            this.textContent = '恢复接收日志';
        } else {
            connect();
            this.textContent = '停止接收日志';
        }
    });

    document.getElementById('clearBtn').addEventListener('click', function () {
        logContainer.innerHTML = '';
        lineNumber = 1; // 清空日志时重置行号
    });

    document.getElementById('downloadBtn').addEventListener('click', function () {
        downloadLogs();
    });

    function displayLog(log) {
        var entry = document.createElement('div');
        entry.textContent = lineNumber + '. [' + log.timestamp + '] ' + log.level + ' ' + log.logger + ' - ' + log.message;
        if (log.exception) {
            var errorEntry = document.createElement('pre');
            errorEntry.className = 'error';
            errorEntry.textContent = log.exception;
            entry.appendChild(errorEntry);
        }
        logContainer.appendChild(entry);
        logContainer.scrollTop = logContainer.scrollHeight;
        lineNumber++; // 增加行号
    }

    function downloadLogs() {
        var logs = logContainer.innerText;
        var blob = new Blob([logs], { type: 'text/plain;charset=utf-8;' });
        var url = URL.createObjectURL(blob);
        var a = document.createElement('a');
        a.href = url;
        a.download = 'logs.txt';
        document.body.appendChild(a);
        a.click();
        document.body.removeChild(a);
        URL.revokeObjectURL(url);
    }

    // 初始连接
    connect();
</script>
</body>
</html>
